<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

/**
 * Description of Upload
 *
 * @author Administrator
 */
class Upload extends CommonController {

    protected $token;
    protected static $host_url;

    function __construct() {
        parent::__construct();
        self::$host_url = 'http://plb76aeo4.bkt.clouddn.com';
        $accessKey = 'dpV3W-l4--EV5sv2oZTDYx0HvQ17YDCnTC7HgNsE';
        $secretKey = 'M3jCegLkt2up6-_5n_LmOsw30Z35wVNXKVlyANHa';
        //上传的空间
        $bucket = 'workchat';
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传 Token
        $this->token = $auth->uploadToken($bucket);
    }

    //文件上传到七牛云
    function qiniu_img() {
        $uploadMgr = new UploadManager();
        //本地上传信息
        $file = $this->request->file('file');
        if (empty($file)) {
            $rs['code'] = '-1';
            $rs['msg'] = '图片上传失败';
            $rs['data'] = '';
            echo json_encode($rs);
            exit;
        }
        $filePath = $file->getRealPath();
        $save_name = md5($filePath) . date('Y-m-d/H:i:s') . rand(1, 99999999);
        list($ret, $err) = $uploadMgr->putFile($this->token, $save_name, $filePath);
        if ($err !== null) {
            $rs['code'] = '-1';
            $rs['msg'] = '图片上传失败,检测上传三方';
            $rs['data'] = '';
        } else {
            $rs['code'] = '0';
            $rs['msg'] = 'success';
            $rs['data'] = array('src' => self::$host_url . '/' . $ret['key']);
        }
        echo json_encode($rs);
    }

    //
    function qiniu_file() {
        $uploadMgr = new UploadManager();
        //本地上传信息
        $file = $this->request->file('file');
        if (empty($file)) {
            $rs['code'] = '-1';
            $rs['msg'] = '文件上传失败';
            $rs['data'] = '';
            echo json_encode($rs);
            exit;
        }
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        $save_name = md5($filePath) . rand(1, 99999999) . '.' . $ext;
        list($ret, $err) = $uploadMgr->putFile($this->token, $save_name, $filePath);
        if ($err !== null) {
            $rs['code'] = '-1';
            $rs['msg'] = '文件上传失败,检测上传三方';
            $rs['data'] = '';
        } else {
            $rs['code'] = '0';
            $rs['msg'] = 'success';
            $rs['data'] = array(
                'src' => self::$host_url . '/' . $ret['key'],
                'name' => $save_name
            );
        }
        echo json_encode($rs);
    }

    //put your code here
    function img() {
        $file = $this->request->file('file');
        $upload = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $filename = 'http://www.chatwork.me/uploads/' . $upload->getSaveName();
        $rs['code'] = '0';
        $rs['msg'] = 'success';
        $rs['data'] = array('src' => $filename);
        echo json_encode($rs);
    }

}
